import { computed, ref, watch } from 'vue'
import dayjs from 'dayjs'
import { getDateSelector } from '@/api/activity'
import {
	ActivityBonusKeys,
	ActivityCashbackKeys,
	ActivityExpireBonusKeys,
	ActivityRebateKeys,
	SlotsRebateKeys,
	SportsRebateKeys
} from '@/enums/activity-reward-type'
import { RewardsEnumDiff, RewardsRecord, RewardStatusEnum } from '@/enums'
import { useActivityStore } from '@/store'
import { listExposeType } from '@/components/base/list/type.ts'

export const useActivityInfo = () => {
	const activityStore = useActivityStore()
	const detailData = computed(() => ({}))
	const type = computed(() => activityStore.rewardPopupData?.type)
	const _bonusType = computed(
		() =>
			RewardsEnumDiff[activityStore.rewardPopupData?.type] || activityStore.rewardPopupData?.type
	)
	// const _bonusType = RewardsEnumDiff[bonusType] || bonusType
	const startTime = dayjs().subtract(30, 'day').startOf('day').format('YYYY-MM-DD HH:mm:ss')
	const endTime = dayjs().endOf('day').format('YYYY-MM-DD HH:mm:ss')
	const listRef = ref<listExposeType>()
	const totalAmount = ref(0)
	const paramType = computed(() => {
		switch (activityStore.rewardPopupData?.type) {
			case 'reward':
			case 'bonus':
			case 'bonusTypes':
				return ActivityBonusKeys
			case 'rebateTypes':
				return ActivityRebateKeys
			case 'sportsRebate':
			case 'sportsRebateKeys':
				return SportsRebateKeys
			case 'slotsRebate':
			case 'slotsRebateKeys':
				return SlotsRebateKeys
			case 'cashback':
			case 'cashbackTypes':
				return ActivityCashbackKeys
			case RewardsRecord.lockBonus.id:
			case RewardsRecord.expireBonus.id:
				return ActivityExpireBonusKeys
			default:
				return [activityStore.rewardPopupData?.type]
		}
	})

	const params: any = ref({
		status: RewardStatusEnum.SUCCESS.id,
		type: paramType.value,
		filterTime: [startTime, endTime],
		// 记录当前选择的天数的key; bonus/selector 的入参
		// bonusType: _bonusType.value,
		// 从二级带入三级页面的参数 包含type 会覆盖前面的type
		...detailData.value
	})
	// 待解锁奖金 在三级详情页 查询的是单个奖金类型
	if ([RewardsRecord.lockBonus.id].includes(type.value)) {
		if ('status' in params.value) {
			delete params.value.status
		}
		// 所有状态的记录.[删除status参数]
		// delete params.value.status
	}
	// 过期奖金 在三级详情页 查询的是合集类型
	if ([RewardsRecord.expireBonus.id].includes(type.value)) {
		params.value.type = ActivityExpireBonusKeys
		// 过期奖金 查询的是 过期状态
		params.value.status = RewardStatusEnum.EXPIRED.id
	}

	const onLoad = async (data: any): Promise<void> => {
		// 待解锁奖金,再次查接口 获取游戏名字
		if ([RewardsRecord.lockBonus.id].includes(type.value)) {
			if (Array.isArray(data?.list)) {
				await activityStore.getUnlockBetRecord(data.list)
			}
		}

		totalAmount.value = data.totalAmount || 0
	}

	const handleSelect = (date: any) => {
		params.value.filterTime[0] = dayjs(date.time).startOf('day').format('YYYY-MM-DD HH:mm:ss')
		params.value.filterTime[1] = endTime
		params.value.dateType = date.id
		if (date.id === 'all') {
			params.value.filterTime = []
		}
		listRef.value?.onRefresh()
	}

	function formatList(list: any) {
		const result = list.reduce((acc: any, curr: any) => {
			acc[curr.type] = acc[curr.type] ? acc[curr.type].concat([curr]) : [curr]
			return acc
		}, {})
		return Object.values(result).map((item: any) => {
			return item?.reduce((acc: any, curr: any) => {
				return { ...curr, amount: curr.amount + (acc.amount || 0) }
			}, {})
		}, [])
	}

	const selectDay = ref('all')
	const updateTime = ref(0)
	const getCurrentStartTime = async () => {
		try {
			const { data } = await getDateSelector(_bonusType.value)
			selectDay.value = data.dateType || 'all'
		} catch (error) {
			console.error(error)
			// showError(error)
		}
		updateTime.value = Date.now()
	}
	const onOpen = async () => {
		await getCurrentStartTime()
	}

	watch(
		() => activityStore.rewardPopupData?.type,
		() => (params.value.type = paramType.value)
	)

	return {
		formatList,
		paramType: paramType.value,
		totalAmount,
		listRef,
		params,
		onLoad,
		onOpen,
		selectDay,
		updateTime,
		handleSelect
	}
}
